package com.nosnow.edu.controller;


import com.nosnow.common.utils.R;
import com.nosnow.edu.entity.EduChapter;
import com.nosnow.edu.entity.chapter.ChapterVo;
import com.nosnow.edu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author bingheng
 * @since 2020-04-04
 */
@Api(description = "课程章节")
@RestController
@RequestMapping("/edu/chapter")
// @CrossOrigin // 解决跨域问题
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @ApiOperation(value="课程大纲列表，根据课程Id进行查询")
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo", list);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @ApiOperation(value = "根据Id查询章节")
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return R.ok().data("chapter", eduChapter);
    }

    @ApiOperation(value = "修改章节")
    @PostMapping("modifyChapter")
    public R modifyChapter(@RequestBody EduChapter eduChapter) {
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    /**
     * 删除章节的业务逻辑：当章节中存在小节时，提示用户，该章节存在小节，不允许删除
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "删除章节")
    @DeleteMapping("removeChapter/{chapterId}")
    public R removeChapter(@PathVariable String chapterId) {
        Boolean flog = eduChapterService.deleteChapter(chapterId);
        if(flog) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

